package com.eduprocess.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eduprocess.entity.City;
import com.eduprocess.entity.EmailVerify;
import com.eduprocess.entity.Province;
import com.eduprocess.entity.Region;
import com.eduprocess.entity.SchoolApplicant;
import com.eduprocess.viewmodel.JsonResult;
import com.octo.captcha.service.image.ImageCaptchaService;

@Controller
public class HomeController {
	private static final Logger logger = Logger.getLogger(HomeController.class);

	@Autowired
	private ImageCaptchaService imageCaptchaService;

	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/")
	public String home() {

		return "index";
	}

	/**
	 * 学校申请管理账号
	 * 
	 * @return
	 */
	@RequestMapping(value = "/signup", method = RequestMethod.GET)
	public String signup(HttpServletRequest request) {

		// 列表省份
		List<Province> provinces = new ArrayList<Province>();
		Province province = new Province();
		province.setProvinceId("010");
		province.setProvinceName("北京");
		provinces.add(province);

		request.setAttribute("provinces", provinces);
		return "signup";
	}

	/**
	 * 管理账号申请处理
	 * 
	 * @param schoolApplicant
	 * @param applicantResult
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/json/signup", method = RequestMethod.POST)
	public @ResponseBody
	JsonResult signup(@Valid SchoolApplicant schoolApplicant, BindingResult applicantResult, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 检查申请账号是否已经注册过
	 * 
	 * @param email
	 * @return
	 */
	@RequestMapping(value = "/json/signup/checkemail")
	public @ResponseBody
	boolean signupCheckEmail(String email) {

		// 检查该Email申请信息的状态
		// 1.未确认用户提示冲突

		// 2.已确认成功用户，提示冲突

		return true;
	}

	/**
	 * 依据省列表城市
	 * 
	 * @param provinceId
	 * @return
	 */
	@RequestMapping(value = "/json/signup/listcity")
	public @ResponseBody
	JsonResult listCity(String provinceId) {
		JsonResult jsonResult = new JsonResult();

		City city = new City();
		city.setCityId("010");
		city.setCityName("北京市");
		List<City> cities = new ArrayList<City>();
		cities.add(city);

		jsonResult.setObj(cities);
		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 依据城市列表区县
	 * 
	 * @param regionId
	 * @return
	 */
	@RequestMapping(value = "/json/signup/listregion")
	public @ResponseBody
	JsonResult listRegion(String cityId) {
		JsonResult jsonResult = new JsonResult();

		Region region = new Region();
		region.setRegionId("0000");
		region.setRegionName("海淀区");
		List<Region> regions = new ArrayList<Region>();
		regions.add(region);

		jsonResult.setObj(regions);
		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 确认手机验证码
	 * 
	 * @param emailVerify
	 * @return
	 */
	@RequestMapping(value = "/json/signup/mobileverify")
	public @ResponseBody
	JsonResult verifyMobile(EmailVerify emailVerify) {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 重新发送手机验证码
	 * 
	 * @param emailVerify
	 * @return
	 */
	@RequestMapping(value = "/json/signup/resendverifycode")
	public @ResponseBody
	JsonResult resendVerifyCode(EmailVerify emailVerify) {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 发送 修改手机号 邮件
	 * 
	 * @param verifyCode
	 * @return
	 */
	@RequestMapping(value = "/json/signup/sendmodifymobileemail")
	public @ResponseBody
	JsonResult sendModifyMobileEmail(String verifyCode) {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 验证码图片
	 * 
	 * @param session
	 * @param os
	 */
	@RequestMapping(value = "/verification")
	public void verification(HttpSession session, OutputStream os) {
		BufferedImage challenge = imageCaptchaService.getImageChallengeForID(session.getId());
		try {
			ImageIO.write(challenge, "JPEG", os);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 登录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login")
	public String login() {
		return "login";
	}

	/**
	 * 教师登录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login/teacher")
	public @ResponseBody
	JsonResult teacherLogin() {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 学生登录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login/student")
	public @ResponseBody
	JsonResult studentLogin() {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 老师登录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login/parent")
	public @ResponseBody
	JsonResult parentLogin() {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}

	/**
	 * 操作主界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/main")
	public String main() {
		return "main";
	}

	/**
	 * 操作主界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/help")
	public String help() {
		return "help";
	}

	/**
	 * 重置密码
	 * 
	 * @return
	 */
	@RequestMapping(value = "/resetpasswd")
	public String resetPassword() {
		return "resetpasswd";
	}

	/**
	 * 发送重置密码邮件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/json/sendresetspasswdemail")
	public @ResponseBody
	JsonResult sentResetPasswdEmail() {
		JsonResult jsonResult = new JsonResult();

		jsonResult.setResult(true);
		return jsonResult;
	}
}
